home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 4_2005-2006.ISO / data / Zips / run-length2014018172006.psc / VB Projects / Common / mGeneral.bas < prev    next >
BASIC Source File  |  2006-08-10  |  22KB  |  745 lines

  1. Attribute VB_Name = "mGeneral"
  2. Option Explicit
  3.  
  4. 'mGeneral.bas by dafhi  August 8, 2006
  5.  
  6. 'Dependency:  FileDlg2.cls
  7.  
  8. 'This module contains declarations that I use alot
  9.  
  10. Type RECT
  11.     Left As Long
  12.     Top As Long
  13.     Right As Long
  14.     Bottom As Long
  15. End Type
  16.  
  17. Type RGBTriple
  18.     Blue As Byte
  19.     Green As Byte
  20.     Red As Byte
  21. End Type
  22.  
  23. Type RGBQUAD
  24.  Blue  As Byte
  25.  Green As Byte
  26.  Red   As Byte
  27.  alpha As Byte
  28. End Type
  29.  
  30. Type Bitmap
  31.     bmType As Long
  32.     bmWidth As Long
  33.     bmHeight As Long
  34.     bmWidthBytes As Long
  35.     bmPlanes As Integer
  36.     bmBitsPixel As Integer
  37.     bmBits As Long
  38. End Type
  39.  
  40. Type SAFEARRAY1D
  41.     cDims As Integer
  42.     fFeatures As Integer
  43.     cbElements As Long
  44.     cLocks As Long
  45.     pvData As Long
  46.     cElements As Long
  47.     lLbound As Long
  48. End Type
  49.  
  50. Private Type SAFEARRAYBOUND
  51.     cElements As Long
  52.     lLbound As Long
  53. End Type
  54. Type SAFEARRAY2D
  55.     cDims As Integer
  56.     fFeatures As Integer
  57.     cbElements As Long
  58.     cLocks As Long
  59.     pvData As Long
  60.     Bounds(1) As SAFEARRAYBOUND
  61. End Type
  62.  
  63. Dim I As Long
  64. Dim J As Long
  65.  
  66. Public Const pi As Double = 3.14159265358979
  67. Public Const TwoPi As Double = 2 * pi
  68. Public Const piBy2 As Single = pi / 2
  69. Public Const halfPi As Single = piBy2
  70.  
  71. Const HWND_TOPMOST = -1
  72. Const SWP_NOMOVE = &H2
  73. Const SWP_NOSIZE = &H1
  74.  
  75. Public Const NOTE_1OF12 As Double = 2 ^ (1 / 12)
  76.  
  77. Public Const ASC_DOUBLE_QUOTE As Integer = 34
  78.  
  79. Dim LBA  As Long
  80. Dim UBA  As Long
  81. Dim LenA As Long
  82.  
  83. Dim mStr       As String
  84. Dim mAsc       As Integer
  85.  
  86. Dim mStrA      As String
  87. Dim mStrB      As String
  88.  
  89. 'ARGBHSV() Function
  90. Public Blu_&
  91. Public Grn_&
  92. Public Red_&
  93. Public subt!
  94.  
  95. Public Const GrayScaleRGB As Long = 1 + 256& + 65536
  96.  
  97. Public Const MaskHIGH       As Long = &HFF0000
  98. Public Const MaskMID        As Long = &HFF00&
  99. Public Const MaskLOW        As Long = &HFF&
  100. Public Const MaskRB         As Long = &HFF00FF
  101. Public Const MaskR          As Long = &HFF0000
  102. Public Const MaskG          As Long = &HFF00&
  103. Public Const MaskB          As Long = &HFF&
  104. Public Const RB_Add1        As Long = &H10001
  105. Public Const G_Add1         As Long = &H100&
  106. Public Const L65536         As Long = 65536
  107. Public Const L256           As Long = 256&
  108.  
  109. 'skew corner
  110. Public g_sk_zoom   As Single
  111. Public g_sk_angle  As Single
  112.  
  113. 'CheckFPS()
  114. Public Tick       As Long
  115. Public FrameCount As Long
  116. Public speed      As Single
  117. Public sFPS       As Single
  118.  
  119. Public PrevTick   As Long
  120. Public NextTick   As Long
  121. Private TickSum   As Long
  122.  
  123. Private Const Interval_Micro As Long = 4
  124.  
  125. Public Const TIME_MARK   As Integer = 256
  126.  
  127. 'Midi standard
  128. Public Const NOTE_ON     As Byte = &H90
  129. Public Const NOTE_OFF    As Byte = &H80
  130. Public Const NOTE_GONE   As Byte = &H81
  131.  
  132. Type BITMAPINFOHEADER
  133.     biSize          As Long
  134.     biWidth         As Long
  135.     biHeight        As Long
  136.     biPlanes        As Integer
  137.     biBitCount      As Integer
  138.     biCompression   As Long
  139.     biSizeImage     As Long
  140.     biXPelsPerMeter As Long
  141.     biYPelsPerMeter As Long
  142.     biClrUsed       As Long
  143.     biClrImportant  As Long
  144. End Type
  145.  
  146. Type BITMAPINFO
  147.     bmiHeader As BITMAPINFOHEADER
  148.     bmiColors As RGBQUAD
  149. End Type
  150.  
  151. 'Private Type BITMAPINFO256 'from www.vbAccelerator.com
  152. '    bmiHeader As BITMAPINFOHEADER
  153. '    bmiColors(0 To 255) As RGBQUAD
  154. 'End Type
  155.  
  156. Declare Function StretchDIBits Lib "gdi32" _
  157.         (ByVal hDC As Long, _
  158.          ByVal x As Long, _
  159.          ByVal y As Long, _
  160.          ByVal dx As Long, _
  161.          ByVal dy As Long, _
  162.          ByVal SrcX As Long, _
  163.          ByVal SrcY As Long, _
  164.          ByVal wSrcWidth As Long, _
  165.          ByVal wSrcHeight As Long, _
  166.          lpBits As Any, _
  167.          lpBitsInfo As BITMAPINFOHEADER, _
  168.          ByVal wUsage As Long, _
  169.          ByVal dwRop As Long) As Long
  170.  
  171. Public Const BI_RGB         As Long = 0
  172. Public Const DIB_RGB_COLORS As Long = 0
  173.  
  174. 'Type LARGE_INTEGER
  175. '    lowpart As Long
  176. '    highpart As Long
  177. 'End Type
  178. 'Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As LARGE_INTEGER) As Long
  179. 'Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As LARGE_INTEGER) As Long
  180. Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long
  181. Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long
  182. Declare Function timeGetTime Lib "winmm.dll" () As Long
  183. Declare Function RedrawWindow Lib "user32" (ByVal hwnd&, lprcUpdate As RECT, ByVal hrgnUpdate&, ByVal fuRedraw&) As Long
  184. Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  185.  
  186. 'SurfaceDescFromFile
  187. Public Declare Function GetObjectAPI Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
  188. Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
  189. Public Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
  190. Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  191. Public Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
  192. Public Declare Function GetPixel Lib "gdi32" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long) As Long
  193.  
  194. 'Public Declare Function CreateDIBSection Lib "gdi32" (ByVal hDC As Long, pBitmapInfo As BITMAPINFO, ByVal un As Long, lpBits As Long, ByVal Handle As Long, ByVal dw As Long) As Long
  195. 'Public Declare Function CreateDIBitmap Lib "gdi32" (ByVal hdc As Long, _
  196.  lpInfoHeader As BITMAPINFOHEADER, _
  197.  ByVal dwUsage As Long, _
  198.  lpInitBits As Any, _
  199.  lpInitInfo As BITMAPINFO, _
  200.  ByVal wUsage As Long) As Long
  201.  
  202. 'from www.vbAccelerator.com
  203. 'Private Declare Function GetDIBits256 Lib "gdi32" Alias "GetDIBits" (ByVal aHDC _
  204.  As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As _
  205.  Long, lpBits As Any, lpBI As BITMAPINFO256, ByVal wUsage As Long) As Long
  206.  
  207. 'Declare Function CreateBitmapIndirect Lib "gdi32" (lpBitmap As Bitmap) As Long
  208. 'Declare Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long, _
  209.  ByVal nHeight As Long, _
  210.  ByVal nPlanes As Long, _
  211.  ByVal nBitCount As Long, _
  212.  lpBits As Any) As Long
  213.  
  214. Public Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, _
  215.  ByVal hBitmap As Long, _
  216.  ByVal nStartScan As Long, _
  217.  ByVal nNumScans As Long, _
  218.  lpBits As Any, _
  219.  lpBI As BITMAPINFO, _
  220.  ByVal wUsage As Long) As Long
  221. Public Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
  222. 'Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
  223. 'Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
  224. 'Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long
  225.  
  226. Declare Function VarPtrArray Lib "msvbvm60" Alias "VarPtr" (Ptr() As Any) As Long
  227. Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy&)
  228.  
  229. Public Type BitmapFileHeader ' 14 bytes
  230.         bfType As Integer
  231.         bfSize As Long
  232.         bfReserved1 As Integer
  233.         bfReserved2 As Integer
  234.         bfOffBits As Long
  235. End Type
  236.  
  237. Public Const BMPFileSignature As Integer = &H4D42
  238.  
  239. Private mDialogShowing As Boolean
  240.  
  241. '****************'
  242. '*              *'
  243. '*   Graphics   *'
  244. '*              *'
  245. '****************'
  246.  
  247. Function GetPadBytes(ByVal PelWidth As Integer, Optional ByVal BytesPixel As Integer = 3, Optional ByVal ByteAlign As Long = 4) As Long
  248.     GetPadBytes = ByteAlign - 1 - (BytesPixel * PelWidth + ByteAlign - 1) Mod ByteAlign
  249. End Function
  250.  
  251.  
  252. Public Function RGBHSV(hue_0_To_1530!, ByVal saturation_0_To_1!, value_0_To_255!) As Long
  253. Dim hue_and_sat As Single
  254. Dim value1      As Single
  255. Dim diff1       As Single
  256. Dim maxim       As Single
  257.  
  258.  If value_0_To_255 > 0 Then
  259.   value1 = value_0_To_255 + 0.5
  260.   If saturation_0_To_1 > 0 Then
  261.    maxim = hue_0_To_1530 - 1530& * Int(hue_0_To_1530 / 1530&)
  262.    diff1 = saturation_0_To_1 * value_0_To_255
  263.    subt = value1 - diff1
  264.    diff1 = diff1 / 255
  265.    If maxim <= 510 Then
  266.     Blu_ = Int(subt)
  267.     If maxim <= 255 Then
  268.      hue_and_sat = maxim * diff1!
  269.      Red_ = Int(value1)
  270.      Grn_ = Int(subt + hue_and_sat)
  271.     Else
  272.      hue_and_sat = (maxim - 255) * diff1!
  273.      Grn_ = Int(value1)
  274.      Red_ = Int(value1 - hue_and_sat)
  275.     End If
  276.    ElseIf maxim <= 1020 Then
  277.     Red_ = Int(subt)
  278.     If maxim <= 765 Then
  279.      hue_and_sat = (maxim - 510) * diff1!
  280.      Grn_ = Int(value1)
  281.      Blu_ = Int(subt + hue_and_sat)
  282.     Else
  283.      hue_and_sat = (maxim - 765) * diff1!
  284.      Blu_ = Int(value1)
  285.      Grn_ = Int(value1 - hue_and_sat)
  286.     End If
  287.    Else
  288.     Grn_ = Int(subt)
  289.     If maxim <= 1275 Then
  290.      hue_and_sat = (maxim - 1020) * diff1!
  291.      Blu_ = Int(value1)
  292.      Red_ = Int(subt + hue_and_sat)
  293.     Else
  294.      hue_and_sat = (maxim - 1275) * diff1!
  295.      Red_ = Int(value1)
  296.      Blu_ = Int(value1 - hue_and_sat)
  297.     End If
  298.    End If
  299.    RGBHSV = Red_ Or Grn_ * 256& Or Blu_ * 65536
  300.   Else 'saturation_0_To_1 <= 0
  301.    RGBHSV = Int(value1) * CLng(65793) '1 + 256 + 65536
  302.   End If
  303.  Else 'value_0_To_255 <= 0
  304.   RGBHSV = 0&
  305.  End If
  306. End Function
  307. Public Function ARGBHSV(hue_0_To_1530!, ByVal saturation_0_To_1!, value_0_To_255!) As Long
  308. Dim hue_and_sat As Single
  309. Dim value1      As Single
  310. Dim diff1       As Single
  311. Dim maxim       As Single
  312.  
  313.  If value_0_To_255 > 0 Then
  314.   value1 = value_0_To_255 + 0.5
  315.   If saturation_0_To_1 > 0 Then
  316.    maxim = hue_0_To_1530 - 1530& * Int(hue_0_To_1530 / 1530&)
  317.    diff1 = saturation_0_To_1 * value_0_To_255
  318.    subt = value1 - diff1
  319.    diff1 = diff1 / 255
  320.    If maxim <= 510 Then
  321.     Blu_ = Int(subt)
  322.     If maxim <= 255 Then
  323.      hue_and_sat = maxim * diff1!
  324.      Red_ = Int(value1)
  325.      Grn_ = Int(subt + hue_and_sat)
  326.     Else
  327.      hue_and_sat = (maxim - 255) * diff1!
  328.      Grn_ = Int(value1)
  329.      Red_ = Int(value1 - hue_and_sat)
  330.     End If
  331.    ElseIf maxim <= 1020 Then
  332.     Red_ = Int(subt)
  333.     If maxim <= 765 Then
  334.      hue_and_sat = (maxim - 510) * diff1!
  335.      Grn_ = Int(value1)
  336.      Blu_ = Int(subt + hue_and_sat)
  337.     Else
  338.      hue_and_sat = (maxim - 765) * diff1!
  339.      Blu_ = Int(value1)
  340.      Grn_ = Int(value1 - hue_and_sat)
  341.     End If
  342.    Else
  343.     Grn_ = Int(subt)
  344.     If maxim <= 1275 Then
  345.      hue_and_sat = (maxim - 1020) * diff1!
  346.      Blu_ = Int(value1)
  347.      Red_ = Int(subt + hue_and_sat)
  348.     Else
  349.      hue_and_sat = (maxim - 1275) * diff1!
  350.      Red_ = Int(value1)
  351.      Blu_ = Int(value1 - hue_and_sat)
  352.     End If
  353.    End If
  354.    ARGBHSV = Red_ * 65536 Or Grn_ * 256& Or Blu_
  355.   Else 'saturation_0_To_1 <= 0
  356.    ARGBHSV = Int(value1) * CLng(65793) '1 + 256 + 65536
  357.   End If
  358.  Else 'value_0_To_255 <= 0
  359.   ARGBHSV = 0&
  360.  End If
  361. End Function
  362. Public Function FlipRB(Color_ As Long) As Long
  363. Dim LBlu As Long
  364.     LBlu = Color_ And &HFF&
  365.     FlipRB = (Color_ And &HFF00&) + 256& * (LBlu * 256&) + (Color_ \ 256&) \ 256&
  366. End Function
  367.  
  368.  
  369. Sub FPS_Init() 'right before game loop
  370.     PrevTick = timeGetTime
  371.     NextTick = PrevTick + Interval_Micro - 1
  372. End Sub
  373. Function CheckFPS(Optional RetFPS, Optional ByVal speed_coefficient As Single = 1, Optional Interval_Millisec& = 200) As Boolean
  374.     
  375. 'CODE SAMPLE
  376. '1. Paste comments below to Form
  377. '2. hit ctrl-h
  378. '3. line 1 says [comment mark][1 space] (2 characters total)
  379. '4. line 2 says nothing
  380. '5. Replace All
  381. '6. be sure to reference mGeneral.bas
  382.     
  383. ' Private Sub Form_Load()
  384.     ' FPS_Init 'initialize time variables
  385.     ' Do While DoEvents '"very simple game loop"
  386.         
  387.         ' Cls
  388.         ' Print "posx = posx + dx * speed
  389.         ' Print "speed is smaller for faster CPU
  390.         
  391.         ' If CheckFPS(FPS, speed_multiplier, 200) Then
  392.         '    Caption = "FPS: " & FPS
  393.         ' End If
  394.     ' Loop
  395. ' End Sub
  396.     
  397.     Tick = timeGetTime
  398.     
  399.     FrameCount = FrameCount + 1
  400.     TickSum = Tick - PrevTick
  401.     speed = speed_coefficient * (TickSum / FrameCount)
  402.     
  403.     If Tick > NextTick Then
  404.         RetFPS = 1000 * FrameCount / TickSum
  405.         sFPS = RetFPS
  406.         NextTick = Tick + Interval_Millisec - 1
  407.         If NextTick < Tick Then NextTick = Tick
  408.         FrameCount = 0
  409.         PrevTick = Tick
  410.         CheckFPS = True
  411.     Else
  412.         CheckFPS = False
  413.     End If
  414.  
  415. End Function
  416.  
  417.  
  418. '********************'
  419. '*                  *'
  420. '*   String stuff   *'
  421. '*                  *'
  422. '********************'
  423.  
  424. Sub FillBytesFromString(Bytes1() As Byte, ByVal Str1 As String)
  425.     LBA = LBound(Bytes1)
  426.     UBA = UBound(Bytes1)
  427.     mStr = Left$(Str1, UBA - LBA + 1)
  428.     For I = LBA To UBA
  429.         Bytes1(I) = Asc(Mid$(mStr, I + 1, 1))
  430.     Next
  431. End Sub
  432. Function StringFromBytes(Bytes() As Byte) As String
  433.     LenA = UBound(Bytes) - LBound(Bytes) + 1
  434.     If LenA > 0 Then
  435.         StringFromBytes = Bytes
  436.         StringFromBytes = StringFromBytes + StringFromBytes
  437.         For I = LBound(Bytes) To UBound(Bytes)
  438.             Mid$(StringFromBytes, I + 1, 1) = Chr$(Bytes(I))
  439.         Next
  440.     End If
  441. End Function
  442. Function GetLine(StrInput As String, ByVal POS_ As Long, Optional RetPos As Long) As String
  443.     If POS_ > Len(StrInput) Then
  444.         GetLine = ""
  445.         RetPos = POS_
  446.         Exit Function
  447.     End If
  448.     For I = POS_ To Len(StrInput)
  449.         J = Asc(Mid$(StrInput, I, 1))
  450.         If I = 10 Or I = 13 Then Exit For
  451.     Next
  452.     GetLine = Mid$(StrInput, POS_, I - POS_)
  453.     RetPos = POS_
  454. End Function
  455. Public Sub NumbersOnly(pTxt As TextBox, Optional pRetVal As Variant, Optional MightUseDollarSign As Boolean, Optional IntegerOnly As Boolean = False, Optional AlwaysPositive As Boolean = False)
  456. Dim PointCount As Integer
  457. Dim MinusCount As Integer
  458. Dim J1 As Long
  459. Dim I1 As Long
  460. Dim bSignFound As Boolean
  461.  
  462.     J1 = 1
  463.     If AlwaysPositive Then MinusCount = 1
  464.     
  465.     If MightUseDollarSign Then
  466.         For I1 = J1 To 1
  467.             mStr = Mid$(pTxt, I1, 1)
  468.             If mStr = "$" Then
  469.                 J1 = 2
  470.                 bSignFound = True
  471.             End If
  472.         Next
  473.     End If
  474.     
  475.     For I1 = J1 To Len(pTxt)
  476.         
  477.         If I1 > Len(pTxt) Then Exit For
  478.     
  479.         mStr = Mid$(pTxt, I1, 1)
  480.         
  481.         If mStr = "-" Then
  482.             
  483.             If MinusCount > 0 Then
  484.                 RemoveChar pTxt, I1, J1
  485.             End If
  486.             
  487.             Add MinusCount, 1
  488.         
  489.         ElseIf mStr = "." Then
  490.             
  491.             If PointCount > 0 Or IntegerOnly Then
  492.                 RemoveChar pTxt, I1, J1
  493.             End If
  494.             
  495.             Add PointCount, 1
  496.         
  497.         Else
  498.         
  499.             mAsc = Asc(mStr)
  500.             
  501.             If mAsc < 48 Or mAsc > 57 Then 'non numeric
  502.                 RemoveChar pTxt, I1, J1
  503.             Else
  504.                 MinusCount = 1
  505.             End If
  506.             
  507.         End If
  508.         
  509.     Next
  510.     
  511.     If Not IsMissing(pRetVal) Then
  512.         If bSignFound Then
  513.             J1 = 2
  514.         Else
  515.             J1 = 1
  516.         End If
  517.         If Len(pTxt) >= J1 Then
  518.             If IsNumeric(pTxt) Then
  519.                 pRetVal = Mid$(pTxt, J1, I1 - J1)
  520.             End If
  521.         End If
  522.     End If
  523.     
  524. End Sub
  525. Private Sub RemoveChar(pTxt As TextBox, pPos As Long, pStart As Long)
  526. Dim lLen As Long
  527.     mStrA = Mid$(pTxt, pStart, pPos - pStart)
  528.     pStart = pPos + 1
  529.     If pStart > Len(pTxt) Then
  530.         mStrB = ""
  531.     Else
  532.         mStrB = Mid$(pTxt, pStart, Len(pTxt) - pStart + 1)
  533.     End If
  534.     pTxt = mStrA + mStrB
  535.     pStart = pPos
  536.     
  537.     Add pPos, -1
  538. End Sub
  539.  
  540.  
  541. ' == File ==
  542. Function IsFile(strFileSpec As String) As Boolean
  543.     If strFileSpec = "" Then Exit Function
  544.     If Len(Dir$(strFileSpec)) > 0 Then
  545.         IsFile = True
  546.     Else
  547.         IsFile = False
  548.     End If
  549. End Function
  550. Function ValidFile(strFullFileSpec As String) As Boolean
  551. Dim FS
  552.     Set FS = CreateObject("Scripting.FileSystemObject")
  553.     ValidFile = FS.fileexists(strFullFileSpec)
  554. End Function
  555.  
  556. ' various math
  557. Sub Add(Varia1 As Variant, ByVal value_ As Double)
  558.     Varia1 = Varia1 + value_
  559. End Sub
  560. Sub mUL(Varia1 As Variant, ByVal value_ As Double)
  561.     Varia1 = Varia1 * value_
  562. End Sub
  563. Sub LinearAlg(ret_ As Single, from_!, to_!, perc_!)
  564.     ret_ = from_ + perc_ * (to_ - from_)
  565. End Sub
  566. Sub TruncVar(ByRef retVal As Variant)
  567.     retVal = retVal - Int(retVal)
  568. End Sub
  569. Function Triangle(ByVal In_dbl#) As Double
  570.     Triangle = In_dbl - Int(In_dbl)
  571.     If Triangle > 0.75 Then
  572.         Triangle = Triangle - 1
  573.     ElseIf Triangle > 0.25 Then
  574.         Triangle = 0.5 - Triangle
  575.     End If
  576. End Function
  577. Function LMax(ByVal sVar1 As Single, ByVal sVar2 As Single) As Long
  578.     If sVar1 < sVar2 Then
  579.         LMax = Int(sVar2 + 0.5)
  580.     Else
  581.         LMax = Int(sVar1 + 0.5)
  582.     End If
  583. End Function
  584. Function LMin(ByVal sVar1 As Single, ByVal sVar2 As Single) As Long
  585.     If sVar1 < sVar2 Then
  586.         LMin = Int(sVar1 + 0.5)
  587.     Else
  588.         LMin = Int(sVar2 + 0.5)
  589.     End If
  590. End Function
  591. Sub SkewCorner(pRetSX!, pRetSY!, ByVal pRad!, ByVal pAngle_0_To_1!, Optional ByVal p_rnd_quadrant_swing_mult! = 0)
  592.     pRad = pRad * g_sk_zoom
  593.     pAngle_0_To_1 = (g_sk_angle + pAngle_0_To_1 + p_rnd_quadrant_swing_mult * (Rnd - 0.5) * 0.25) * TwoPi
  594.     pRetSX = pRad * Cos(pAngle_0_To_1)
  595.     pRetSY = pRad * Sin(pAngle_0_To_1)
  596. End Sub
  597. Sub SngModulus(ByRef retVal As Variant, ByVal pMod As Single)
  598.     If pMod = 0 Then Exit Sub
  599.     retVal = retVal - pMod * Int(retVal / pMod)
  600. End Sub
  601. Sub RadianModulus(ByRef retAngle As Variant)
  602.  retAngle = retAngle - TwoPi * Int(retAngle / TwoPi)
  603. End Sub
  604. Function TriangleModulus(ByVal in1 As Single, ByVal modulus As Single) As Single
  605. Dim mod4!
  606.   
  607.     mod4 = modulus * 4
  608.     
  609.     'mod operation
  610.     TriangleModulus = in1 - mod4 * Int(in1 / mod4)
  611.     
  612.     'triangle constraint
  613.     If TriangleModulus > modulus * 3 Then
  614.         TriangleModulus = TriangleModulus - mod4
  615.     ElseIf TriangleModulus > modulus Then
  616.         TriangleModulus = modulus * 2 - TriangleModulus
  617.     End If
  618.   
  619. End Function
  620. Function ModulusTriPositive(ByVal in1 As Single, ByVal modulus As Single) As Single
  621. Dim mod2!
  622.   
  623.     mod2 = modulus * 2
  624.     
  625.     'mod operation
  626.     ModulusTriPositive = in1 - mod2 * Int(in1 / mod2)
  627.     
  628.     If ModulusTriPositive > modulus Then ModulusTriPositive = mod2 - ModulusTriPositive
  629.   
  630. End Function
  631. Function RndPosNeg() As Long
  632.     RndPosNeg = 2 * Int(Rnd - 0.5) + 1
  633. End Function
  634. Function GetAngle(sngDX!, sngDY!) As Single
  635.  If sngDY = 0! Then
  636.   If sngDX < 0! Then
  637.    GetAngle = pi * (3& / 2&)
  638.   ElseIf sngDX > 0 Then
  639.    GetAngle = pi / 2!
  640.   End If
  641.  Else
  642.   If sngDY > 0! Then
  643.    GetAngle = pi - Atn(sngDX / sngDY)
  644.   Else
  645.    GetAngle = Atn(sngDX / -sngDY)
  646.   End If
  647.  End If
  648. End Function
  649. Function GetAngle2(sngDX!, sngDY!) As Single
  650.  If sngDX = 0! Then
  651.   If sngDY < 0! Then
  652.    GetAngle2 = pi * 1.5!
  653.   ElseIf sngDY > 0 Then
  654.    GetAngle2 = pi * 0.5!
  655.   End If
  656.  Else
  657.   If sngDX > 0! Then
  658.    GetAngle2 = Atn(sngDY / sngDX)
  659.   Else
  660.    GetAngle2 = pi - Atn(sngDY / -sngDX)
  661.   End If
  662.  End If
  663. End Function
  664. Public Sub swap(pVar1 As Variant, pVar2 As Variant)
  665. Dim lVar3 As Variant
  666.  
  667.     lVar3 = pVar1
  668.     pVar1 = pVar2
  669.     pVar2 = lVar3
  670.  
  671. End Sub
  672. Public Function DialogSuccess(pFileSpec As String, Optional ByVal pIsLoad As Boolean = True, Optional pRetFileName As String, Optional pRetDir As String = "", Optional ByVal pForceExtension As String = "", Optional pRetFreeFile As Integer) As Boolean
  673. Dim CDLF As OSDialog
  674.  
  675. DialogSuccess = False
  676.  
  677.     'Experimental
  678.  
  679.     If mDialogShowing Then Exit Function
  680.     
  681.     pRetFileName = pFileSpec
  682.  
  683.     If Left$(pForceExtension, 1) <> "." Then pForceExtension = "." & pForceExtension
  684.     
  685.     Set CDLF = New OSDialog
  686.     
  687.     For I = Len(pRetFileName) To 1 Step -1
  688.         If Mid$(pRetFileName, I, 1) = "\" Then
  689.             CDLF.Directory = Left$(pRetFileName, I - 1)
  690.             Exit For
  691.         End If
  692.     Next
  693.     
  694.     mDialogShowing = True
  695.     
  696.     mStr = "*" & pForceExtension
  697.     
  698.     If pIsLoad Then
  699.         CDLF.ShowOpen pRetFileName, , "(" & mStr & ")|" & mStr, pRetDir
  700.         mDialogShowing = False
  701.         If Not IsFile(pRetFileName) Then
  702.             Set CDLF = Nothing
  703.             Exit Function
  704.         End If
  705.     Else
  706.         If CDLF.ShowSave(pRetFileName, , "(" & mStr & ")|" & mStr, pRetDir, pForceExtension) = "" Then
  707.             mDialogShowing = False
  708.             Set CDLF = Nothing
  709.             Exit Function
  710.         Else
  711.             mDialogShowing = False
  712.         End If
  713.     End If
  714.     
  715.     For I = 1 To Len(pRetFileName)
  716.         If Mid$(pRetFileName, I, 1) = "." Then
  717.             If Len(pForceExtension) > 1 Then
  718.                 pRetFileName = Left$(pRetFileName, I - 1) & pForceExtension
  719.             Else
  720.                 pForceExtension = Right$(pRetFileName, Len(pRetFileName) - I + 1)
  721.             End If
  722.             Exit For
  723.         End If
  724.     Next
  725.     
  726.     If Right$(pRetFileName, Len(pForceExtension)) <> pForceExtension Then
  727.         pRetFileName = pRetFileName & pForceExtension
  728.     End If
  729.     
  730.     If Len(pRetFileName) > Len(pForceExtension) Then
  731.     
  732.         pRetFileName = pRetFileName
  733.         pRetDir = CDLF.Directory
  734.         pFileSpec = pRetDir & pRetFileName
  735.                 
  736.         pRetFreeFile = FreeFile
  737.  
  738.         DialogSuccess = True
  739.         
  740.     End If
  741.     
  742.     Set CDLF = Nothing
  743.  
  744. End Function
  745.